<?php



/*
* File: SimpleImage.php
* Author: Simon Jarvis
* Copyright: 2006 Simon Jarvis
* Date: 08/11/06
* Link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
* 
* This program is free software; you can redistribute it and/or 
* modify it under the terms of the GNU General Public License 
* as published by the Free Software Foundation; either version 2 
* of the License, or (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful, 
* but WITHOUT ANY WARRANTY; without even the implied warranty of 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
* GNU General Public License for more details: 
* http://www.gnu.org/licenses/gpl.html
*
*/
 
class SimpleImage {
   
   var $image;
   var $image_type;
 
   function load($filename) {
      $image_info = getimagesize($filename);
      $this->image_type = $image_info[2];
      if( $this->image_type == IMAGETYPE_JPEG ) {
         $this->image = imagecreatefromjpeg($filename);
      } elseif( $this->image_type == IMAGETYPE_GIF ) {
         $this->image = imagecreatefromgif($filename);
      } elseif( $this->image_type == IMAGETYPE_PNG ) {
         $this->image = imagecreatefrompng($filename);
      }
   }
   function save($filename, $image_type=IMAGETYPE_JPEG, $compression=100, $permissions=null) {
      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this->image,$filename,100);
      } elseif( $image_type == IMAGETYPE_GIF ) {
         imagegif($this->image,$filename);         
      } elseif( $image_type == IMAGETYPE_PNG ) {
         imagepng($this->image,$filename);
      }   
      if( $permissions != null) {
         chmod($filename,$permissions);
      }
   }
   function output($image_type=IMAGETYPE_JPEG) {
      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this->image, null, 100);
      } elseif( $image_type == IMAGETYPE_GIF ) {
         imagegif($this->image);         
      } elseif( $image_type == IMAGETYPE_PNG ) {
         imagepng($this->image);
      }   
   }
   function getWidth() {
      return imagesx($this->image);
   }
   function getHeight() {
      return imagesy($this->image);
   }
   function resizeToHeight($height) {
      $ratio = $height / $this->getHeight();
      $width = $this->getWidth() * $ratio;
      $this->resize($width,$height);
   }
   function resizeToWidth($width) {
      $ratio = $width / $this->getWidth();
      $height = $this->getheight() * $ratio;
      $this->resize($width,$height);
   }
   function scale($scale) {
      $width = $this->getWidth() * $scale/100;
      $height = $this->getheight() * $scale/100; 
      $this->resize($width,$height);
   }
   #

   function resize($width,$height) {
   	if ($this->getWidth() < $this->getHeight()){
      	$tmpW = $width;
      	$width = $height;
      	$height = $tmpW;
      }
      $new_image = imagecreatetruecolor($width, $height);
      
      
      
       $wm = $this->getWidth()/$width;
	    $hm = $this->getHeight()/$height;

	    $h_height = $height/2;
	    $w_height = $width/2;

	    /*if($this->getWidth() > $this->getHeight()) {

	        $adjusted_width = $this->getWidth() / $hm;

	        $half_width = $adjusted_width / 2;

	        $int_width = $half_width - $w_height;
	        
	        $adjusted_height = $this->getHeight() / $wm;
	
	        $half_height = $adjusted_height / 2;
	
	        $int_height = $half_height - $h_height;

	        imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $adjusted_width, $height, $this->getWidth(), $this->getHeight());

	    } elseif(($this->getWidth() < $this->getHeight()) || ($this->getWidth() == $this->getHeight())) {
	
	    	$adjusted_width = $this->getWidth() / $hm;

	        $half_width = $adjusted_width / 2;

	        $int_width = $half_width - $w_height;
	    	
			$adjusted_height = $this->getHeight() / $wm;
	
	        $half_height = $adjusted_height / 2;
	
	        $int_height = $half_height - $h_height;
	
	        imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $adjusted_height, $this->getWidth(), $this->getHeight());
	 	} else {*/
	
	        imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
	
	    /*}
      */
      $this->image = $new_image;   
   }      
}



class ImageUtils
{
	static function resize($source, $target, $maxx, $maxy) {
		if ( !file_exists($source) )
			throw new ImageException('Plik nie istnieje');
		$ext = substr($source, -3);
		
		switch ( $ext ) {
			case 'jpg':
				$img = imagecreatefromjpeg($source);
				break;
			case 'gif':
				$img = imagecreatefromgif($source);
				break;
			default:
				throw new ImageException('Nie obsługiwany format pliku: '.$ext);
		}

		$x = imagesx($img);
		$y = imagesy($img);

		if( $x>$maxx || $y>$maxy ) {
			if($x > $y) {
				$nx = $maxx;
				$ny = $maxx * ($y / $x);
			}
			else if($x < $y) {
				$nx = $maxy * ($x / $y);
				$ny = $maxy;
			}
			else {
				$nx = $maxy;
				$ny = $maxy;
			}
			if ( $nx == $maxx && $ny > $maxy ) {
				$nx = $maxy * ($x / $y);
				$ny = $maxy;
			}
	
			$thumb = imagecreatetruecolor($nx, $ny);
			imagecopyresized($thumb, $img, 0, 0, 0, 0, $nx, $ny, $x, $y);
			
			switch ( $ext ) {
				case 'jpg':
					imagejpeg($thumb, $target, 90);
					break;
				case 'gif':
					imagegif($thumb, $target);
					break;
			}
		}
		else {
			copy($source, $target);
		}
	}
}


class ImageThumb {
	public function hasExtension($name){
		return $this->extension==$name;
	}
	
	
	public function getImgPath($destinationDir, $width = '', $height = ''){

			$filename = $destinationDir.'/'.$this->id.'.'.$this->extension;
			if(file_exists($filename)){
				$img_attr = getimagesize($destinationDir.'/'.$this->id.'.'.$this->extension);
				$max_width = $width;
				$max_height = $height;
				
				
				$ext = $this->extension;
				/*
				if(!$max_width || !$max_height){
					if(!$max_width){	
						$scale = $img_attr[1] / $max_height;
						$max_width = $img_attr[0] / $scale;
					}
					else {
						$scale = $img_attr[0] / $max_width;
						$max_height = $img_attr[1] / $scale;
					}
				}
				else
					$scale = $img_attr[0] / $max_width;
				*/

				$w = $max_width;
				$h = $max_height;
				
				
				$im = imagecreatetruecolor($w,$h);
				$background = imagecolorallocate($im, 255, 255, 255);
				imagefilledrectangle($im, 0, 0, $w, $h, $background);		
				/*
				while($img_attr[1]/$scale < $max_width){
					$max_height ++;
					$scale = $img_attr[1] / $max_height;
				}
				*/
			
				$w = $max_width;
				$h = $max_height;
				
				switch($ext){
					case 'png' :{
							imagecopyresampled($im, imagecreatefrompng($filename), 0, 0, 0, 0, $w, $h, $img_attr[0], $img_attr[1]);
							imagepng($im, DEV_IMAGES_CACHE_DIR.'/'.$fileName.'-'.$thumbwidth.'-'.$thumbheight.'.'.$fileExtension);
						}
					break;
					
					case 'gif' :{
							imagecopyresampled($im, imagecreatefromgif($filename), 0, 0, 0, 0, $w, $h, $img_attr[0], $img_attr[1]);
							imagegif($im, DEV_IMAGES_CACHE_DIR.'/'.$fileName.'-'.$thumbwidth.'-'.$thumbheight.'.'.$fileExtension);
						}
					break;
					
					case 'jpeg':{
							imagecopyresampled($im, imagecreatefromjpeg($filename), 0, 0, 0, 0, $w, $h, $img_attr[0], $img_attr[1]);
							imagejpeg($im, DEV_IMAGES_CACHE_DIR.'/'.$fileName.'-'.$thumbwidth.'-'.$thumbheight.'.'.$fileExtension, 100);
						}
					break;
					
					case 'jpg':{
							imagecopyresampled($im, imagecreatefromjpeg($filename), 0, 0, 0, 0, $w, $h, $img_attr[0], $img_attr[1]);
							imagejpeg($im, DEV_IMAGES_CACHE_DIR.'/'.$fileName.'-'.$thumbwidth.'-'.$thumbheight.'.'.$fileExtension, 100);
						}
					break;
					
					default:{
						imagecopyresampled($im, imagecreatefromjpeg($filename), 0, 0, 0, 0, $w, $h, $img_attr[0], $img_attr[1]);
						imagejpeg($im, DEV_IMAGES_CACHE_DIR.'/'.$fileName.'-'.$thumbwidth.'-'.$thumbheight.'.'.$fileExtension, 100);
					}
				}
				imagedestroy($im);
				return DEV_IMAGES_CACHE_DIR.'/'.$this->id-$width-$height.'.'.$this->extension;
			}
			else{
				return $this->getConfig()->no_file_image;	
			}			
	}
	
	public static function createThumb($fileName, $fileExtension, $destinationDir, $thumbwidth, $thumbheight, $method = 'zc', $show = true)
	{
		if(file_exists(str_replace('//','/',$_SERVER['DOCUMENT_ROOT'] . DEV_IMAGES_CACHE_DIR) . '/t'.$fileName.'-'.$thumbwidth.'-'.$thumbheight.'.'.$fileExtension) && $show == true){
			return DEV_IMAGES_CACHE_DIR .'/t'. $fileName.'-'.$thumbwidth.'-'.$thumbheight.'.'.$fileExtension;
		}
		else{
			$targetPath = $_SERVER['DOCUMENT_ROOT'] . $destinationDir . '/';
			//$targetPath = $destinationDir . '/';
			$image =  str_replace('//','/',$targetPath) . $fileName;
			//echo $image;
			if (!file_exists($image)){
				$targetPath = $_SERVER['DOCUMENT_ROOT'] . DEV_ICON_NO_FILE;
				//$image = str_replace('//','/',$targetPath);
				$image = DEV_ICON_NO_FILE;
				return $image;
			}
			
			$thumbname = str_replace('//','/',$_SERVER['DOCUMENT_ROOT'] . DEV_IMAGES_CACHE_DIR ).'/t'.$fileName.'-'.$thumbwidth.'-'.$thumbheight.'.'.$fileExtension;
			$zoom = .0; // zoom percentage
			$imgdata = getimagesize($image);
			
			if($imgdata[0] <= $thumbwidth && $imgdata[1] <= $thumbheight){
				$thumbwidth = $imgdata[0];
				$thumbheight = $imgdata[1];
			}
			
			
			$imgratio = $imgdata[0] / $imgdata[1];  // width/height
			$thumbratio = $thumbwidth / $thumbheight;
			switch($method) {
				case "sc": // scale
				case "zs": // zoom & scale
					$src_x = 0;
					$src_y = 0;
					$src_w = $imgdata[0];
					$src_h = $imgdata[1];
					if( $imgratio > $thumbratio )
					{
						$newwidth = $thumbwidth;
						$newheight = ceil($thumbwidth / $imgratio);
					}
					else
					{
						$newheight = $thumbheight;
						$newwidth = ceil($thumbheight * $imgratio);
					}
					break;
	
				case "cr": // crop
				case "zc": // zoom & crop
					$newwidth = $thumbwidth;
					$newheight = $thumbheight;
					if( $imgratio > $thumbratio )
					{
						$src_x = ceil(($imgdata[0] - $imgdata[1] * $thumbratio) / 2);
						$src_y = 0;
						$src_w = $imgdata[0] - $src_x * 2;
						$src_h = $imgdata[1];
					}
					else
					{
						$src_x = 0;
						$src_y = ceil(($imgdata[1] - $imgdata[0] / $thumbratio) / 3);
						$src_w = $imgdata[0];
						$src_h = $imgdata[1] - $src_y * 3;
					}
					break;
			}
			if ( $method == "zs" || $method == "zc" )
			{
				$src_x = $src_x + ceil($zoom / 2 * $src_w);
				$src_y = $src_y + ceil($zoom / 3 * $src_h);
				$src_w = ceil($src_w * (1-$zoom));
				$src_h = ceil($src_h * (1-$zoom));
			}
			if ( file_exists($thumbname) )
			{
				$thumbdata = getimagesize($thumbname);
			}
			if ( !isset($thumbdata) || $thumbdata[0] != $newwidth || $thumbdata[1] != $newheight )
			{
				$newimage = imagecreatetruecolor($newwidth,$newheight);
				switch($imgdata['mime']) {
					case "image/gif":
						$source = imagecreatefromgif($image);
						break;
					case "image/pjpeg":
					case "image/jpeg":
					case "image/jpg":
						$source = imagecreatefromjpeg($image);
						break;
					case "image/png":
					case "image/x-png":
						$source = imagecreatefrompng($image);
						break;
					}
				imagecopyresampled($newimage,$source,0,0,$src_x,$src_y,$newwidth,$newheight,$src_w,$src_h);
				switch($imgdata['mime']) {
					case "image/gif":
							imagegif($newimage,$thumbname);
						break;
					case "image/pjpeg":
					case "image/jpeg":
					case "image/jpg":
							imagejpeg($newimage,$thumbname,80);
						break;
					case "image/png":
					case "image/x-png":
						imagepng($newimage,$thumbname);
						break;
					}
				imagedestroy($newimage);
				chmod($thumbname, 0777);
			}
			
			if ($show == true){
				return DEV_IMAGES_CACHE_DIR . '/t'.$fileName.'-'.$thumbwidth.'-'.$thumbheight.'.'.$fileExtension;
			}
		}
		
	}
	
	public static function getMimeType($ext){ 
		$mimeTypes = array("323" => "text/h323",
						"acx" => "application/internet-property-stream",
						"ai" => "application/postscript",
						"aif" => "audio/x-aiff",
						"aifc" => "audio/x-aiff",
						"aiff" => "audio/x-aiff",
						"asf" => "video/x-ms-asf",
						"asr" => "video/x-ms-asf",
						"asx" => "video/x-ms-asf",
						"au" => "audio/basic",
						"avi" => "video/x-msvideo",
						"axs" => "application/olescript",
						"bas" => "text/plain",
						"bcpio" => "application/x-bcpio",
						"bin" => "application/octet-stream",
						"bmp" => "image/bmp",
						"c" => "text/plain",
						"cat" => "application/vnd.ms-pkiseccat",
						"cdf" => "application/x-cdf",
						"cer" => "application/x-x509-ca-cert",
						"class" => "application/octet-stream",
						"clp" => "application/x-msclip",
						"cmx" => "image/x-cmx",
						"cod" => "image/cis-cod",
						"cpio" => "application/x-cpio",
						"crd" => "application/x-mscardfile",
						"crl" => "application/pkix-crl",
						"crt" => "application/x-x509-ca-cert",
						"csh" => "application/x-csh",
						"css" => "text/css",
						"dcr" => "application/x-director",
						"der" => "application/x-x509-ca-cert",
						"dir" => "application/x-director",
						"dll" => "application/x-msdownload",
						"dms" => "application/octet-stream",
						"doc" => "application/msword",
						"dot" => "application/msword",
						"dvi" => "application/x-dvi",
						"dxr" => "application/x-director",
						"eps" => "application/postscript",
						"etx" => "text/x-setext",
						"evy" => "application/envoy",
						"exe" => "application/octet-stream",
						"fif" => "application/fractals",
						"flr" => "x-world/x-vrml",
						"gif" => "image/gif",
						"gtar" => "application/x-gtar",
						"gz" => "application/x-gzip",
						"h" => "text/plain",
						"hdf" => "application/x-hdf",
						"hlp" => "application/winhlp",
						"hqx" => "application/mac-binhex40",
						"hta" => "application/hta",
						"htc" => "text/x-component",
						"htm" => "text/html",
						"html" => "text/html",
						"htt" => "text/webviewhtml",
						"ico" => "image/x-icon",
						"ief" => "image/ief",
						"iii" => "application/x-iphone",
						"ins" => "application/x-internet-signup",
						"isp" => "application/x-internet-signup",
						"jfif" => "image/pipeg",
						"jpe" => "image/jpeg",
						"jpeg" => "image/jpeg",
						"jpg" => "image/jpeg",
						"js" => "application/x-javascript",
						"latex" => "application/x-latex",
						"lha" => "application/octet-stream",
						"lsf" => "video/x-la-asf",
						"lsx" => "video/x-la-asf",
						"lzh" => "application/octet-stream",
						"m13" => "application/x-msmediaview",
						"m14" => "application/x-msmediaview",
						"m3u" => "audio/x-mpegurl",
						"man" => "application/x-troff-man",
						"mdb" => "application/x-msaccess",
						"me" => "application/x-troff-me",
						"mht" => "message/rfc822",
						"mhtml" => "message/rfc822",
						"mid" => "audio/mid",
						"mny" => "application/x-msmoney",
						"mov" => "video/quicktime",
						"movie" => "video/x-sgi-movie",
						"mp2" => "video/mpeg",
						"mp3" => "audio/mpeg",
						"mpa" => "video/mpeg",
						"mpe" => "video/mpeg",
						"mpeg" => "video/mpeg",
						"mpg" => "video/mpeg",
						"mpp" => "application/vnd.ms-project",
						"mpv2" => "video/mpeg",
						"ms" => "application/x-troff-ms",
						"mvb" => "application/x-msmediaview",
						"nws" => "message/rfc822",
						"oda" => "application/oda",
						"p10" => "application/pkcs10",
						"p12" => "application/x-pkcs12",
						"p7b" => "application/x-pkcs7-certificates",
						"p7c" => "application/x-pkcs7-mime",
						"p7m" => "application/x-pkcs7-mime",
						"p7r" => "application/x-pkcs7-certreqresp",
						"p7s" => "application/x-pkcs7-signature",
						"pbm" => "image/x-portable-bitmap",
						"pdf" => "application/pdf",
						"pfx" => "application/x-pkcs12",
						"pgm" => "image/x-portable-graymap",
						"pko" => "application/ynd.ms-pkipko",
						"pma" => "application/x-perfmon",
						"pmc" => "application/x-perfmon",
						"pml" => "application/x-perfmon",
						"pmr" => "application/x-perfmon",
						"pmw" => "application/x-perfmon",
						"pnm" => "image/x-portable-anymap",
						"pot" => "application/vnd.ms-powerpoint",
						"ppm" => "image/x-portable-pixmap",
						"pps" => "application/vnd.ms-powerpoint",
						"ppt" => "application/vnd.ms-powerpoint",
						"prf" => "application/pics-rules",
						"ps" => "application/postscript",
						"pub" => "application/x-mspublisher",
						"qt" => "video/quicktime",
						"ra" => "audio/x-pn-realaudio",
						"ram" => "audio/x-pn-realaudio",
						"ras" => "image/x-cmu-raster",
						"rgb" => "image/x-rgb",
						"rmi" => "audio/mid",
						"roff" => "application/x-troff",
						"rtf" => "application/rtf",
						"rtx" => "text/richtext",
						"scd" => "application/x-msschedule",
						"sct" => "text/scriptlet",
						"setpay" => "application/set-payment-initiation",
						"setreg" => "application/set-registration-initiation",
						"sh" => "application/x-sh",
						"shar" => "application/x-shar",
						"sit" => "application/x-stuffit",
						"snd" => "audio/basic",
						"spc" => "application/x-pkcs7-certificates",
						"spl" => "application/futuresplash",
						"src" => "application/x-wais-source",
						"sst" => "application/vnd.ms-pkicertstore",
						"stl" => "application/vnd.ms-pkistl",
						"stm" => "text/html",
						"svg" => "image/svg+xml",
						"sv4cpio" => "application/x-sv4cpio",
						"sv4crc" => "application/x-sv4crc",
						"t" => "application/x-troff",
						"tar" => "application/x-tar",
						"tcl" => "application/x-tcl",
						"tex" => "application/x-tex",
						"texi" => "application/x-texinfo",
						"texinfo" => "application/x-texinfo",
						"tgz" => "application/x-compressed",
						"tif" => "image/tiff",
						"tiff" => "image/tiff",
						"tr" => "application/x-troff",
						"trm" => "application/x-msterminal",
						"tsv" => "text/tab-separated-values",
						"txt" => "text/plain",
						"uls" => "text/iuls",
						"ustar" => "application/x-ustar",
						"vcf" => "text/x-vcard",
						"vrml" => "x-world/x-vrml",
						"wav" => "audio/x-wav",
						"wcm" => "application/vnd.ms-works",
						"wdb" => "application/vnd.ms-works",
						"wks" => "application/vnd.ms-works",
						"wmf" => "application/x-msmetafile",
						"wps" => "application/vnd.ms-works",
						"wri" => "application/x-mswrite",
						"wrl" => "x-world/x-vrml",
						"wrz" => "x-world/x-vrml",
						"xaf" => "x-world/x-vrml",
						"xbm" => "image/x-xbitmap",
						"xla" => "application/vnd.ms-excel",
						"xlc" => "application/vnd.ms-excel",
						"xlm" => "application/vnd.ms-excel",
						"xls" => "application/vnd.ms-excel",
						"xlt" => "application/vnd.ms-excel",
						"xlw" => "application/vnd.ms-excel",
						"xof" => "x-world/x-vrml",
						"xpm" => "image/x-xpixmap",
						"xwd" => "image/x-xwindowdump",
						"z" => "application/x-compress",
						"zip" => "application/zip"
					);
					
		return $mimeTypes[$ext];
	}
	
	public static function generateDirs($hash){
		$t1 = substr($hash,0,4);
		$t2 = substr($hash,4,4);
		$t3 = substr($hash,8,4);
		$t4 = substr($hash,12,4);
		$t5 = substr($hash,16,4);
		$t6 = substr($hash,20,4);
		$t7 = substr($hash,24,4);
		$t8 = substr($hash,28,4);
		
		if(!is_dir(DEV_IMAGES_CACHE_DIR.$t1)){
			mkdir(DEV_IMAGES_CACHE_DIR.$t1);
		}
		if(!is_dir(DEV_IMAGES_CACHE_DIR.$t1.'/'.$t2)){
			mkdir(DEV_IMAGES_CACHE_DIR.$t1.'/'.$t2);
		}
		if(!is_dir(DEV_IMAGES_CACHE_DIR.$t1.'/'.$t2.'/'.$t3)){
			mkdir(DEV_IMAGES_CACHE_DIR.$t1.'/'.$t2.'/'.$t3);
		}
		if(!is_dir(DEV_IMAGES_CACHE_DIR.$t1.'/'.$t2.'/'.$t3.'/'.$t4)){
			mkdir(DEV_IMAGES_CACHE_DIR.$t1.'/'.$t2.'/'.$t3.'/'.$t4);
		}
		if(!is_dir(DEV_IMAGES_CACHE_DIR.$t1.'/'.$t2.'/'.$t3.'/'.$t4.'/'.$t5)){
			mkdir(DEV_IMAGES_CACHE_DIR.$t1.'/'.$t2.'/'.$t3.'/'.$t4.'/'.$t5);
		}
		if(!is_dir(DEV_IMAGES_CACHE_DIR.$t1.'/'.$t2.'/'.$t3.'/'.$t4.'/'.$t5.'/'.$t6)){
			mkdir(DEV_IMAGES_CACHE_DIR.$t1.'/'.$t2.'/'.$t3.'/'.$t4.'/'.$t5.'/'.$t6);
		}
		if(!is_dir(DEV_IMAGES_CACHE_DIR.$t1.'/'.$t2.'/'.$t3.'/'.$t4.'/'.$t5.'/'.$t6.'/'.$t7)){
			mkdir(DEV_IMAGES_CACHE_DIR.$t1.'/'.$t2.'/'.$t3.'/'.$t4.'/'.$t5.'/'.$t6.'/'.$t7);
		}
		if(!is_dir(DEV_IMAGES_CACHE_DIR.$t1.'/'.$t2.'/'.$t3.'/'.$t4.'/'.$t5.'/'.$t6.'/'.$t7.'/'.$t8)){
			mkdir(DEV_IMAGES_CACHE_DIR.$t1.'/'.$t2.'/'.$t3.'/'.$t4.'/'.$t5.'/'.$t6.'/'.$t7.'/'.$t8);
		}
		
		return $t1.'/'.$t2.'/'.$t3.'/'.$t4.'/'.$t5.'/'.$t6.'/'.$t7.'/'.$t8;
	}
	
	public static function getDirs($hash){
		$t1 = substr($hash,0,4);
		$t2 = substr($hash,4,4);
		$t3 = substr($hash,8,4);
		$t4 = substr($hash,12,4);
		$t5 = substr($hash,16,4);
		$t6 = substr($hash,20,4);
		$t7 = substr($hash,24,4);
		$t8 = substr($hash,28,4);
		
		return $t1.'/'.$t2.'/'.$t3.'/'.$t4.'/'.$t5.'/'.$t6.'/'.$t7.'/'.$t8;
	}
	
	public static function delDirs($hash){
		$t1 = substr($hash,0,4);
		$t2 = substr($hash,4,4);
		$t3 = substr($hash,8,4);
		$t4 = substr($hash,12,4);
		$t5 = substr($hash,16,4);
		$t6 = substr($hash,20,4);
		$t7 = substr($hash,24,4);
		$t8 = substr($hash,28,4);
		
		if(is_dir(DEV_IMAGES_CACHE_DIR.'/'.$t1.'/'.$t2.'/'.$t3.'/'.$t4.'/'.$t5.'/'.$t6.'/'.$t7.'/'.$t8)){
			@rmdir(DEV_IMAGES_CACHE_DIR.'/'.$t1.'/'.$t2.'/'.$t3.'/'.$t4.'/'.$t5.'/'.$t6.'/'.$t7.'/'.$t8);
		}
		if(is_dir(DEV_IMAGES_CACHE_DIR.'/'.$t1.'/'.$t2.'/'.$t3.'/'.$t4.'/'.$t5.'/'.$t6.'/'.$t7)){
			@rmdir(DEV_IMAGES_CACHE_DIR.'/'.$t1.'/'.$t2.'/'.$t3.'/'.$t4.'/'.$t5.'/'.$t6.'/'.$t7);
		}
		if(is_dir(DEV_IMAGES_CACHE_DIR.'/'.$t1.'/'.$t2.'/'.$t3.'/'.$t4.'/'.$t5.'/'.$t6)){
			@rmdir(DEV_IMAGES_CACHE_DIR.'/'.$t1.'/'.$t2.'/'.$t3.'/'.$t4.'/'.$t5.'/'.$t6);
		}
		if(is_dir(DEV_IMAGES_CACHE_DIR.'/'.$t1.'/'.$t2.'/'.$t3.'/'.$t4.'/'.$t5)){
			@rmdir(DEV_IMAGES_CACHE_DIR.'/'.$t1.'/'.$t2.'/'.$t3.'/'.$t4.'/'.$t5);
		}
		if(is_dir(DEV_IMAGES_CACHE_DIR.'/'.$t1.'/'.$t2.'/'.$t3.'/'.$t4)){
			@rmdir(DEV_IMAGES_CACHE_DIR.'/'.$t1.'/'.$t2.'/'.$t3.'/'.$t4);
		}
		if(is_dir(DEV_IMAGES_CACHE_DIR.'/'.$t1.'/'.$t2.'/'.$t3)){
			@rmdir(DEV_IMAGES_CACHE_DIR.'/'.$t1.'/'.$t2.'/'.$t3);
		}
		if(is_dir(DEV_IMAGES_CACHE_DIR.'/'.$t1.'/'.$t2)){
			@rmdir(DEV_IMAGES_CACHE_DIR.'/'.$t1.'/'.$t2);
		}
		if(is_dir(DEV_IMAGES_CACHE_DIR.'/'.$t1)){
			@rmdir(DEV_IMAGES_CACHE_DIR.'/'.$t1);
		}
		
	}
	
	public static function clearFilename($filename){
		$dict = array(
			"√Ä" => "A",
			"√Ā" => "A",
			"√ā" => "A",
			"√É" => "A",
			"√Ą" => "A",
			"√Ö" => "A",
			"√Ü" => "A",
			"ńÄ" => "A",
			"ńĄ" => "A",
			"ńā" => "A",
			"√á" => "C",
			"ńÜ" => "C",
			"ńĆ" => "C",
			"ńą" => "C",
			"ńä" => "C",
			"ńé" => "D",
			"ńź" => "D",
			"√ą" => "E",
			"√Č" => "E",
			"√ä" => "E",
			"√č" => "E",
			"ńí" => "E",
			"ńė" => "E",
			"ńö" => "E",
			"ńĒ" => "E",
			"ńĖ" => "E",
			"ńú" => "G",
			"ńě" => "G",
			"ń†" => "G",
			"ńĘ" => "G",
			"ń§" => "H",
			"ń¶" => "H",
			"√Ć" => "I",
			"√ć" => "I",
			"√é" => "I",
			"√Ź" => "I",
			"ń™" => "I",
			"ń®" => "I",
			"ń¨" => "I",
			"ńģ" => "I",
			"ńį" => "I",
			"ń≤" => "IJ",
			"ńī" => "J",
			"ń∂" => "K",
			"ńĹ" => "K",
			"ńĻ" => "K",
			"ńĽ" => "K",
			"ńŅ" => "K",
			"ŇĀ" => "L",
			"√Ď" => "N",
			"ŇÉ" => "N",
			"Ňá" => "N",
			"ŇÖ" => "N",
			"Ňä" => "N",
			"√í" => "O",
			"√ď" => "O",
			"√Ē" => "O",
			"√ē" => "O",
			"√Ė" => "O",
			"√ė" => "O",
			"ŇĆ" => "O",
			"Ňź" => "O",
			"Ňé" => "O",
			"Ňí" => "OE",
			"ŇĒ" => "R",
			"Ňė" => "R",
			"ŇĖ" => "R",
			"Ňö" => "S",
			"Ňě" => "S",
			"Ňú" => "S",
			"»ė" => "S",
			"Ň†" => "S",
			"Ň§" => "T",
			"ŇĘ" => "T",
			"Ň¶" => "T",
			"»ö" => "T",
			"√ô" => "U",
			"√ö" => "U",
			"√Õ" => "U",
			"√ú" => "Ue",
			"Ň™" => "U",
			"Ňģ" => "U",
			"Ňį" => "U",
			"Ň¨" => "U",
			"Ň®" => "U",
			"Ň≤" => "U",
			"Ňī" => "W",
			"Ň∂" => "Y",
			"Ňł" => "Y",
			"√Ě" => "Y",
			"ŇĻ" => "Z",
			"ŇĽ" => "Z",
			"ŇĹ" => "Z",
			"√†" => "a",
			"√°" => "a",
			"√Ę" => "a",
			"√£" => "a",
			"√§" => "a",
			"ńĀ" => "a",
			"ńÖ" => "a",
			"ńÉ" => "a",
			"√•" => "a",
			"√¶" => "ae",
			"√ß" => "c",
			"ńá" => "c",
			"ńć" => "c",
			"ńČ" => "c",
			"ńč" => "c",
			"ńŹ" => "d",
			"ńĎ" => "d",
			"√®" => "e",
			"√©" => "e",
			"√™" => "e",
			"√ę" => "e",
			"ńď" => "e",
			"ńô" => "e",
			"ńÕ" => "e",
			"ńē" => "e",
			"ńó" => "e",
			"∆í" => "f",
			"ńĚ" => "g",
			"ńü" => "g",
			"ń°" => "g",
			"ń£" => "g",
			"ń•" => "h",
			"ńß" => "h",
			"√¨" => "i",
			"√≠" => "i",
			"√ģ" => "i",
			"√Į" => "i",
			"ńę" => "i",
			"ń©" => "i",
			"ń≠" => "i",
			"ńĮ" => "i",
			"ńĪ" => "i",
			"ń≥" => "ij",
			"ńĶ" => "j",
			"ń∑" => "k",
			"ńł" => "k",
			"Ňā" => "l",
			"ńĺ" => "l",
			"ńļ" => "l",
			"ńľ" => "l",
			"ŇÄ" => "l",
			"√Ī" => "n",
			"ŇĄ" => "n",
			"Ňą" => "n",
			"ŇÜ" => "n",
			"ŇČ" => "n",
			"Ňč" => "n",
			"√≤" => "o",
			"√≥" => "o",
			"√ī" => "o",
			"√Ķ" => "o",
			"√∂" => "o",
			"√ł" => "o",
			"Ňć" => "o",
			"ŇĎ" => "o",
			"ŇŹ" => "o",
			"Ňď" => "oe",
			"Ňē" => "r",
			"Ňô" => "r",
			"Ňó" => "r",
			"ŇÕ" => "s",
			"Ň°" => "s",
			"Ňü" => "s",
			"Ň•" => "t",
			"Ň£" => "t",
			"√Ļ" => "u",
			"√ļ" => "u",
			"√Ľ" => "u",
			"√ľ" => "ue",
			"Ňę" => "u",
			"ŇĮ" => "u",
			"ŇĪ" => "u",
			"Ň≠" => "u",
			"Ň©" => "u",
			"Ň≥" => "u",
			"ŇĶ" => "w",
			"√Ņ" => "y",
			"√Ĺ" => "y",
			"Ň∑" => "y",
			"Ňľ" => "z",
			"Ňļ" => "z",
			"Ňĺ" => "z",
			"√ü" => "ss",
			"ŇŅ" => "ss",
			"őĎ" => "A",
			"őÜ" => "A",
			"Šľą" => "A",
			"ŠľČ" => "A",
			"Šľä" => "A",
			"Šľč" => "A",
			"ŠľĆ" => "A",
			"Šľć" => "A",
			"Šľé" => "A",
			"ŠľŹ" => "A",
			"Šĺą" => "A",
			"ŠĺČ" => "A",
			"Šĺä" => "A",
			"Šĺč" => "A",
			"ŠĺĆ" => "A",
			"Šĺć" => "A",
			"Šĺé" => "A",
			"ŠĺŹ" => "A",
			"Šĺł" => "A",
			"ŠĺĻ" => "A",
			"Šĺļ" => "A",
			"ŠĺĽ" => "A",
			"Šĺľ" => "A",
			"őí" => "B",
			"őď" => "G",
			"őĒ" => "D",
			"őē" => "E",
			"őą" => "E",
			"Šľė" => "E",
			"Šľô" => "E",
			"Šľö" => "E",
			"ŠľÕ" => "E",
			"Šľú" => "E",
			"ŠľĚ" => "E",
			"ŠŅČ" => "E",
			"ŠŅą" => "E",
			"őĖ" => "Z",
			"őó" => "I",
			"őČ" => "I",
			"Šľ®" => "I",
			"Šľ©" => "I",
			"Šľ™" => "I",
			"Šľę" => "I",
			"Šľ¨" => "I",
			"Šľ≠" => "I",
			"Šľģ" => "I",
			"ŠľĮ" => "I",
			"Šĺė" => "I",
			"Šĺô" => "I",
			"Šĺö" => "I",
			"ŠĺÕ" => "I",
			"Šĺú" => "I",
			"ŠĺĚ" => "I",
			"Šĺě" => "I",
			"Šĺü" => "I",
			"ŠŅä" => "I",
			"ŠŅč" => "I",
			"ŠŅĆ" => "I",
			"őė" => "TH",
			"őô" => "I",
			"őä" => "I",
			"ő™" => "I",
			"Šľł" => "I",
			"ŠľĻ" => "I",
			"Šľļ" => "I",
			"ŠľĽ" => "I",
			"Šľľ" => "I",
			"ŠľĹ" => "I",
			"Šľĺ" => "I",
			"ŠľŅ" => "I",
			"ŠŅė" => "I",
			"ŠŅô" => "I",
			"ŠŅö" => "I",
			"ŠŅÕ" => "I",
			"őö" => "K",
			"őÕ" => "L",
			"őú" => "M",
			"őĚ" => "N",
			"őě" => "KS",
			"őü" => "O",
			"őĆ" => "O",
			"ŠĹą" => "O",
			"ŠĹČ" => "O",
			"ŠĹä" => "O",
			"ŠĹč" => "O",
			"ŠĹĆ" => "O",
			"ŠĹć" => "O",
			"ŠŅł" => "O",
			"ŠŅĻ" => "O",
			"ő†" => "P",
			"ő°" => "R",
			"ŠŅ¨" => "R",
			"ő£" => "S",
			"ő§" => "T",
			"ő•" => "Y",
			"őé" => "Y",
			"őę" => "Y",
			"ŠĹô" => "Y",
			"ŠĹÕ" => "Y",
			"ŠĹĚ" => "Y",
			"ŠĹü" => "Y",
			"ŠŅ®" => "Y",
			"ŠŅ©" => "Y",
			"ŠŅ™" => "Y",
			"ŠŅę" => "Y",
			"ő¶" => "F",
			"őß" => "X",
			"ő®" => "PS",
			"ő©" => "O",
			"őŹ" => "O",
			"ŠĹ®" => "O",
			"ŠĹ©" => "O",
			"ŠĹ™" => "O",
			"ŠĹę" => "O",
			"ŠĹ¨" => "O",
			"ŠĹ≠" => "O",
			"ŠĹģ" => "O",
			"ŠĹĮ" => "O",
			"Šĺ®" => "O",
			"Šĺ©" => "O",
			"Šĺ™" => "O",
			"Šĺę" => "O",
			"Šĺ¨" => "O",
			"Šĺ≠" => "O",
			"Šĺģ" => "O",
			"ŠĺĮ" => "O",
			"ŠŅļ" => "O",
			"ŠŅĽ" => "O",
			"ŠŅľ" => "O",
			"őĪ" => "a",
			"ő¨" => "a",
			"ŠľÄ" => "a",
			"ŠľĀ" => "a",
			"Šľā" => "a",
			"ŠľÉ" => "a",
			"ŠľĄ" => "a",
			"ŠľÖ" => "a",
			"ŠľÜ" => "a",
			"Šľá" => "a",
			"ŠĺÄ" => "a",
			"ŠĺĀ" => "a",
			"Šĺā" => "a",
			"ŠĺÉ" => "a",
			"ŠĺĄ" => "a",
			"ŠĺÖ" => "a",
			"ŠĺÜ" => "a",
			"Šĺá" => "a",
			"ŠĹį" => "a",
			"ŠĹĪ" => "a",
			"Šĺį" => "a",
			"ŠĺĪ" => "a",
			"Šĺ≤" => "a",
			"Šĺ≥" => "a",
			"Šĺī" => "a",
			"Šĺ∂" => "a",
			"Šĺ∑" => "a",
			"ő≤" => "b",
			"ő≥" => "g",
			"őī" => "d",
			"őĶ" => "e",
			"ő≠" => "e",
			"Šľź" => "e",
			"ŠľĎ" => "e",
			"Šľí" => "e",
			"Šľď" => "e",
			"ŠľĒ" => "e",
			"Šľē" => "e",
			"ŠĹ≤" => "e",
			"ŠĹ≥" => "e",
			"ő∂" => "z",
			"ő∑" => "i",
			"őģ" => "i",
			"Šľ†" => "i",
			"Šľ°" => "i",
			"ŠľĘ" => "i",
			"Šľ£" => "i",
			"Šľ§" => "i",
			"Šľ•" => "i",
			"Šľ¶" => "i",
			"Šľß" => "i",
			"Šĺź" => "i",
			"ŠĺĎ" => "i",
			"Šĺí" => "i",
			"Šĺď" => "i",
			"ŠĺĒ" => "i",
			"Šĺē" => "i",
			"ŠĺĖ" => "i",
			"Šĺó" => "i",
			"ŠĹī" => "i",
			"ŠĹĶ" => "i",
			"ŠŅā" => "i",
			"ŠŅÉ" => "i",
			"ŠŅĄ" => "i",
			"ŠŅÜ" => "i",
			"ŠŅá" => "i",
			"őł" => "th",
			"őĻ" => "i",
			"őĮ" => "i",
			"Ōä" => "i",
			"őź" => "i",
			"Šľį" => "i",
			"ŠľĪ" => "i",
			"Šľ≤" => "i",
			"Šľ≥" => "i",
			"Šľī" => "i",
			"ŠľĶ" => "i",
			"Šľ∂" => "i",
			"Šľ∑" => "i",
			"ŠĹ∂" => "i",
			"ŠĹ∑" => "i",
			"ŠŅź" => "i",
			"ŠŅĎ" => "i",
			"ŠŅí" => "i",
			"ŠŅď" => "i",
			"ŠŅĖ" => "i",
			"ŠŅó" => "i",
			"őļ" => "k",
			"őĽ" => "l",
			"őľ" => "m",
			"őĹ" => "n",
			"őĺ" => "ks",
			"őŅ" => "o",
			"ŌĆ" => "o",
			"ŠĹÄ" => "o",
			"ŠĹĀ" => "o",
			"ŠĹā" => "o",
			"ŠĹÉ" => "o",
			"ŠĹĄ" => "o",
			"ŠĹÖ" => "o",
			"ŠĹł" => "o",
			"ŠĹĻ" => "o",
			"ŌÄ" => "p",
			"ŌĀ" => "r",
			"ŠŅ§" => "r",
			"ŠŅ•" => "r",
			"ŌÉ" => "s",
			"Ōā" => "s",
			"ŌĄ" => "t",
			"ŌÖ" => "y",
			"Ōć" => "y",
			"Ōč" => "y",
			"őį" => "y",
			"ŠĹź" => "y",
			"ŠĹĎ" => "y",
			"ŠĹí" => "y",
			"ŠĹď" => "y",
			"ŠĹĒ" => "y",
			"ŠĹē" => "y",
			"ŠĹĖ" => "y",
			"ŠĹó" => "y",
			"ŠĹļ" => "y",
			"ŠĹĽ" => "y",
			"ŠŅ†" => "y",
			"ŠŅ°" => "y",
			"ŠŅĘ" => "y",
			"ŠŅ£" => "y",
			"ŠŅ¶" => "y",
			"ŠŅß" => "y",
			"ŌÜ" => "f",
			"Ōá" => "x",
			"Ōą" => "ps",
			"ŌČ" => "o",
			"Ōé" => "o",
			"ŠĹ†" => "o",
			"ŠĹ°" => "o",
			"ŠĹĘ" => "o",
			"ŠĹ£" => "o",
			"ŠĹ§" => "o",
			"ŠĹ•" => "o",
			"ŠĹ¶" => "o",
			"ŠĹß" => "o",
			"Šĺ†" => "o",
			"Šĺ°" => "o",
			"ŠĺĘ" => "o",
			"Šĺ£" => "o",
			"Šĺ§" => "o",
			"Šĺ•" => "o",
			"Šĺ¶" => "o",
			"Šĺß" => "o",
			"ŠĹľ" => "o",
			"ŠĹĹ" => "o",
			"ŠŅ≤" => "o",
			"ŠŅ≥" => "o",
			"ŠŅī" => "o",
			"ŠŅ∂" => "o",
			"ŠŅ∑" => "o",
			"¬®" => "",
			"őÖ" => "",
			"ŠĺŅ" => "",
			"ŠŅĺ" => "",
			"ŠŅć" => "",
			"ŠŅĚ" => "",
			"ŠŅé" => "",
			"ŠŅě" => "",
			"ŠŅŹ" => "",
			"ŠŅü" => "",
			"ŠŅÄ" => "",
			"ŠŅĀ" => "",
			"őĄ" => "",
			"ŠŅģ" => "",
			"ŠŅĮ" => "",
			"ŠŅ≠" => "",
			"Õļ" => "",
			"ŠĺĹ" => "",
			"–ź" => "A",
			"–Ď" => "B",
			"–í" => "V",
			"–ď" => "G",
			"–Ē" => "D",
			"–ē" => "E",
			"–Ā" => "E",
			"–Ė" => "ZH",
			"–ó" => "Z",
			"–ė" => "I",
			"–ô" => "I",
			"–ö" => "K",
			"–Õ" => "L",
			"–ú" => "M",
			"–Ě" => "N",
			"–ě" => "O",
			"–ü" => "P",
			"–†" => "R",
			"–°" => "S",
			"–Ę" => "T",
			"–£" => "U",
			"–§" => "F",
			"–•" => "KH",
			"–¶" => "TS",
			"–ß" => "CH",
			"–®" => "SH",
			"–©" => "SHCH",
			"–ę" => "Y",
			"–≠" => "E",
			"–ģ" => "YU",
			"–Į" => "YA",
			"–į" => "A",
			"–Ī" => "B",
			"–≤" => "V",
			"–≥" => "G",
			"–ī" => "D",
			"–Ķ" => "E",
			"—Ď" => "E",
			"–∂" => "ZH",
			"–∑" => "Z",
			"–ł" => "I",
			"–Ļ" => "I",
			"–ļ" => "K",
			"–Ľ" => "L",
			"–ľ" => "M",
			"–Ĺ" => "N",
			"–ĺ" => "O",
			"–Ņ" => "P",
			"—Ä" => "R",
			"—Ā" => "S",
			"—ā" => "T",
			"—É" => "U",
			"—Ą" => "F",
			"—Ö" => "KH",
			"—Ü" => "TS",
			"—á" => "CH",
			"—ą" => "SH",
			"—Č" => "SHCH",
			"—č" => "Y",
			"—ć" => "E",
			"—é" => "YU",
			"—Ź" => "YA",
			"–™" => "",
			"—ä" => "",
			"–¨" => "",
			"—Ć" => "",
			"√į" => "d",
			"√ź" => "D",
			"√ĺ" => "th",
			"√ě" => "TH",
			"ą" => "a",
			"Ą" => "a",
			"ć" => "c",
			"Ć" => "c",
			"ś" => "s",
			"Ś" => "s",
			"ż" => "z",
			"Ż" => "z",
			"ź" => "z",
			"Ź" => "z",
			"ę" => "e",
			"E" => "e",
			"ó" => "o",
			"O" => "o",
			"ł" => "l",
			"Ł" => "l",
			"ń" => "n",
			"Ń" => "n",
			"&" => "-",
			" " => "-",
			"!" => "-",
			"@" => "-",
			"$" => "-",
			"%" => "-",
			"^" => "-",
			"*" => "-",
			"(" => "-",
			")" => "-",
			"{" => "-",
			"[" => "-",
			"}" => "-",
			"]" => "-",
			"?" => "-",
			"Ä…" => "a",
			"Ĺ‚" => "l",
			"Ĺ›" => "s",
			"Ä‡" => "c",
			"Ĺş" => "z",
			"ĹĽ" => "z",
			"Ăł" => "o"
		);
		
		$new = $filename;
		foreach ($dict as $key => $val){
			$new = str_replace($key, $val, $new);
		}
		
		return $new;
	}
}
?>